package org.test.bookpub.web;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.test.bookpub.bean.Demo;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value = "/demos")
public class DemoController {

	// 创建线程安全的Map
	static Map<Long, Demo> demos = Collections.synchronizedMap(new HashMap<Long, Demo>());

	@ApiOperation(value = "删除demo", notes = "根据url的id来指定删除对象")
	@ApiImplicitParam(name = "id", value = "Demo ID", required = true, dataType = "Long")
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	public String deleteDemo(@PathVariable Long id) {
		// 处理"/demos/{id}"的DELETE请求，用来删除Demo
		DemoController.demos.remove(id);
		return "success";
	}

	@ApiOperation(value = "获取Demo详细信息", notes = "根据url的id来获取Demo详细信息")
	@ApiImplicitParam(name = "id", value = "Demo ID", required = true, dataType = "Long")
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public Demo getDemo(@PathVariable Long id) {
		// 处理"/demos/{id}"的GET请求，用来获取url中id值的Demo信息
		// url中的id可通过@PathVariable绑定到函数的参数中
		return DemoController.demos.get(id);
	}

	@ApiOperation(value = "获取所有的Demo", notes = "根据url的id来获取demo")
	@RequestMapping(value = "", method = RequestMethod.GET)
	public List<Demo> getDemoList() {
		// 处理"/demos/"的GET请求，用来获取用户列表
		// 还可以通过@RequestParam从页面中传递参数来进行查询条件或者翻页信息的传递
		List<Demo> r = new ArrayList<>(DemoController.demos.values());
		return r;
	}

	@ApiOperation(value = "创建Demo", notes = "根据Demo对象创建用户")
	@ApiImplicitParam(name = "demo", value = "demo详细实体", required = true, dataType = "Demo")
	@RequestMapping(value = "", method = RequestMethod.POST)
	public String postDemo(@ModelAttribute Demo demo) {
		// 处理"/demos/"的POST请求，用来创建Demo
		// 除了@ModelAttribute绑定参数之外，还可以通过@RequestParam从页面中传递参数
		DemoController.demos.put(demo.getId(), demo);
		return "success";
	}

	@ApiOperation(value = "更新Demo详细信息", notes = "根据url的id来指定更新对象，并根据传过来的Demo信息来更新Demo详细信息")
	@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "Demo ID", required = true, dataType = "Long"),
			@ApiImplicitParam(name = "demo", value = "Demo详细实体", required = true, dataType = "Demo") })
	@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
	public String putDemo(@PathVariable Long id, @ModelAttribute Demo demo) {
		// 处理"/demos/{id}"的PUT请求，用来更新Demo信息
		Demo u = DemoController.demos.get(id);
		u.setName(demo.getName());
		u.setAge(demo.getAge());
		DemoController.demos.put(id, u);
		return "success";
	}

	@RequestMapping(value = "/testException", method = RequestMethod.GET)
	public String testException() {
		Demo demo = new Demo();
		return demo.getName().trim();
	}
}
